package com.base.kotlin

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.ActorScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import org.junit.Test
import kotlin.system.measureTimeMillis

class Text {
    @InternalCoroutinesApi
    @Test
    fun add() {
        main<Int>()
    }


    @InternalCoroutinesApi
    fun<T> main() = runBlocking {






//          launch(Dispatchers.Default) {
//              println("1")
//              delay(1000)
//              println("2")
//          }

//        val channel = Channel<Int>(capacity =Channel.UNLIMITED )
//
//
//        val produce = GlobalScope.launch {
//            var i = 0
//            while (true) {
//                delay(1000)
//                i++
//                println("before send $i")
//                channel.send(i)
//                println("after send $i")
//            }
//
//        }
//
//        val cousmmer = GlobalScope.launch {
//            while (true) {
//                delay(2000)
//                val element = channel.receive()
//                println("receive $element")
//            }
//        }
//        produce.join()
//        cousmmer.join()
//        val handler = CoroutineExceptionHandler { _, exception ->
//            println("CoroutineExceptionHandler got $exception")
//        }
//        val job = GlobalScope.launch(handler) { // 根协程，运行在 GlobalScope 中
//            throw AssertionError()
//        }
//        val deferred = GlobalScope.async(handler) { // 同样是根协程，但使用 async 代替了 launch
//            throw ArithmeticException() // 没有打印任何东西，依赖用户去调用 deferred.await()
//        }
// try {
//     deferred.await()
// }catch ( e:ArithmeticException){
//
// }
//
//        delay(5000)
        //joinAll(job, deferred)
//        val time= measureTimeMillis {
//            val searchOne = searchOne()
//            val searchTwo = searchTwo()
//            println("the items  is $searchOne and $searchTwo")
//        }
        //ActorScope

//        val time = measureTimeMillis {
//            val  one =async {
//                searchOne()
//            }
//            val  two =async {
//                searchTwo()
//            }
//            println("the items  is ${one} and ${two}")
//        }
//        println("time is  $time ms")
//
//
//
//        delay(4000)
//        val job = GlobalScope.launch {
//            delay(1000)// launch 根协程
//            println("Throwing exception from launch")
//            throw IndexOutOfBoundsException() // 我们将在控制台打印 Thread.defaultUncaughtExceptionHandler
//        }
//        job.join()
//        println("Joined failed job")
//        val deferred = GlobalScope.async { // async 根协程
//            delay(500)
//            println("Throwing exception from async")
//            throw ArithmeticException() // 没有打印任何东西，依赖用户去调用等待
//        }
//        try {
//            deferred.await()
//            println("Unreached")
//        } catch (e: ArithmeticException) {
//            println("Caught ArithmeticException")
//        }

    }


    suspend fun searchOne(): String {
        delay(1000)
        return "item-one"
    }

    suspend fun searchTwo(): String {
        delay(1000)
        return "item-two"
    }
}